package com.shenmazg6.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 军哥
 * @version 1.0
 * @description: RabbitMqConfig
 * @date 2022/12/20 11:45
 */

@Configuration
public class RabbitMqConfig {

    // 订单超时死信队列
    @Bean
    public Queue deadOrderOutQueue() {
        return new Queue("dead-orderout");
    }

    // 死信交换机
    @Bean
    public DirectExchange deadOrderOutExchange() {
        return new DirectExchange("dead-orderout-exchange");
    }

    // 把死信队列绑定到死信交换机上
    @Bean
    public Binding bindDeadTimeOutExchange() {
        return BindingBuilder.bind(deadOrderOutQueue()).to(deadOrderOutExchange()).with("timeout");
    }

    //

    /**
     * 创建订单队列，这个队列是没有消费者
     * @return
     */
    @Bean
    public Queue orderQueue() {
        Map<String, Object> args = new HashMap<>(2);
        //--1 过期时间20s
        args.put("x-message-ttl", 1000*60*5);

        //--2 x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", "dead-orderout-exchange");

        //--3x-dead-letter-routing-key  这里声明当前队列的死信路由key
        args.put("x-dead-letter-routing-key", "timeout");

        Queue businessQueue = QueueBuilder.durable("orderQueue").withArguments(args).build();
        return businessQueue;
    }
}
